package com.lenovo.manufacture.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.lenovo.manufacture.R;
import com.lenovo.manufacture.bean.CaiGouBean;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;

public class ShoppingCarActivity extends AppCompatActivity implements View.OnClickListener {

    private Button ivBack;
    private TextView tvTitle;
    private TextView tvShopSum;
    private Button btShopPrice;
    private Button btShopNum;
    private ListView lvShopList;
    private boolean priceState;
    private boolean numState;
    private Drawable up;
    private Drawable down;
    private List<CaiGouBean.DataBean> tmp = CaiGouActivity.car;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_car);
        initView();
    }

    private void initView() {
        ivBack = (Button) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvShopSum = (TextView) findViewById(R.id.tv_shop_sum);
        btShopPrice = (Button) findViewById(R.id.bt_shop_price);
        btShopNum = (Button) findViewById(R.id.bt_shop_num);
        lvShopList = (ListView) findViewById(R.id.lv_shop_list);

        tvTitle.setText("已购商品");
        ivBack.setOnClickListener(this);
        btShopPrice.setOnClickListener(this);
        btShopNum.setOnClickListener(this);


        adapter = new MyAdapter();
        lvShopList.setAdapter(adapter);

        up = getResources().getDrawable(R.drawable.sanjao3);
        down = getResources().getDrawable(R.drawable.sanjao4);
        up.setBounds(0, 0, up.getMinimumWidth(), up.getMinimumHeight());
        down.setBounds(0, 0, up.getMinimumWidth(), up.getMinimumHeight());

        int sum = 0;
        for (CaiGouBean.DataBean bean : tmp) {
            sum += bean.getPrice();
        }

        DecimalFormat format = new DecimalFormat(",###");
        tvShopSum.setText(format.format(sum));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_shop_price:
                if (priceState) {
                    btShopPrice.setCompoundDrawables(null, null, up, null);
                    Collections.sort(tmp, (o1, o2) -> o1.getPrice() - o2.getPrice());
                } else {
                    btShopPrice.setCompoundDrawables(null, null, down, null);
                    Collections.sort(tmp, (o1, o2) -> o2.getPrice() - o1.getPrice());
                }
                priceState = !priceState;
                adapter.notifyDataSetChanged();
                break;
            case R.id.bt_shop_num:
                if (numState) {
                    btShopNum.setCompoundDrawables(null, null, up, null);
                    Collections.sort(tmp, (o1, o2) -> o1.getNum() - o2.getNum());
                } else {
                    btShopNum.setCompoundDrawables(null, null, down, null);
                    Collections.sort(tmp, (o1, o2) -> o2.getNum() - o1.getNum());
                }
                numState = !numState;
                adapter.notifyDataSetChanged();
                break;
        }
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return tmp.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(getApplicationContext(), R.layout.item_cg, null);
            } else {
                view = convertView;
            }

            TextView tvCgId = view.findViewById(R.id.tv_cg_id);
            TextView tvCgName = view.findViewById(R.id.tv_cg_name);
            TextView tvCgGys = view.findViewById(R.id.tv_cg_gys);
            TextView tvCgPrice = view.findViewById(R.id.tv_cg_price);
            TextView tvCgNum = view.findViewById(R.id.tv_cg_num);

            tvCgId.setText(tmp.get(position).getId() + "");
            tvCgName.setText(tmp.get(position).getMaterialName() + "");
            tvCgGys.setText(tmp.get(position).getSupplyName() + "");
            tvCgPrice.setText(tmp.get(position).getPrice() + "");
            tvCgNum.setText(tmp.get(position).getNum() + "");
            return view;
        }
    }
}
